//初始化云数据库
wx.cloud.init()
const db = wx.cloud.database()

Page({
  data: {
    objectId:'',
    days:[],
    signUp:[],
    test:[],
    cur_year:0,
    cur_month:0,
    cur_day:0,
    count:0,
    flag:false
  },


  onLoad: function () {

    // this.setData({objectId : options.objectId}); 
    //获取当前年月  
    const date = new Date();
    const cur_year = date.getFullYear();
    const cur_month = date.getMonth() + 1;
    const cur_day = date.getDate();
    const weeks_ch = ['日', '一', '二', '三', '四', '五', '六'];
    // this.calculateEmptyGrids(cur_year, cur_month);
    // this.calculateDays(cur_year, cur_month);
    //获取当前用户当前任务的签到状态
    this.onGetSignUp();
    this.setData({
      cur_year,
      cur_month,
      cur_day,
      weeks_ch
    })

  },

  //匹配判断当月与当月哪些日子签到打卡
  onJudgeSign:function(){
    var that = this;
    var signs = that.data.signUp;
    var daysArr = that.data.days;
    var dayflag = false;
    for (var i=0; i < signs.length;i++){
      var current = new Date(signs[i].date.replace(/-/g, "/"));
      var year = current.getFullYear();
      var month = current.getMonth()+1;
      var day = current.getDate();
      day = parseInt(day);
      // for (var j = 0; j < daysArr.length;j++){
        //年月日相同并且已打卡        
        if (year == that.data.cur_year && month == that.data.cur_month && day == that.data.cur_day){
          dayflag = true;
        }        
    }
    that.setData({days:daysArr,flag:dayflag});
    
  },

  //获取当前用户该任务的签到数组
  onGetSignUp:function(){  
    var that = this
    db.collection('task_user').get({
      success: function (res) {
        console.log(res.data[0]);
        that.setData({
          // signUp: res.data[0].signUp,
          count: res.data[0].count
        });
        //获取到值后就判断签到情况
      //  that.onJudgeSign();
      }
    });
    db.collection('task_signUp').get({
      success: function (res) {
        console.log("test: ", res.data);
        that.setData({
          signUp: res.data
          // signUp: res.data
        })
        //获取到值后就判断签到情况
        that.onJudgeSign();
      }
    })

  },

  //跳转到新闻详情
  toSearch: function (e) {
    wx.navigateTo({
      url: '../search/search'
    });
  },

  //实现签到功能
  check: function(){
    var that = this;
    //将当前日期用-分隔
    var myDate = new Date();
    var data = myDate.toLocaleDateString().split('/').join('-');
    var isSign = '今日已打卡';
    var count = parseInt(that.data.count) + 1;
    var arr = {'date':data,'isSign':isSign};
    // console.log("arr: ", arr);
    // console.log("count: ", count);
    //插入到数据库中
    db.collection('task_user').doc('67d68141-c594-4a51-aeb9-4cfc830b8a0c').update({
      // data 传入需要局部更新的数据
      data: {
        // 表示将 done 字段置为 true
        count: count
      },
      success: function (res) {
        console.log(res.data)
      }
    });
    db.collection('task_signUp').add({
      // data 字段表示需新增的 JSON 数据
      data: {        
        date:data,
        isSign:isSign,
        uid:''
      },
      success: function (res) {
        // res 是一个对象，其中有 _id 字段标记刚创建的记录的 id
        console.log(res)
        //刷新当前页
        that.onLoad();
        //重新跳转到日历界面
        wx.reLaunch({
          url: '../calendar/calendar',
        });
      }
    });

  }


})
